import 'package:flutter/material.dart';

//动态组件传值步骤:
// 1.定义个数据传递给父组件
// 2.父组件使用并传递数据给子组件
// 3.在子组件使用initState获取父组件传递过来的数据
// 4.在子组件使用setState更新数据
class myGoodsInfo extends StatefulWidget {
  myGoodsInfo({super.key, required this.orderInfo});
  Map orderInfo = {};
  @override
  State<myGoodsInfo> createState() => _myGoodsInfoState();
}

class _myGoodsInfoState extends State<myGoodsInfo> {
  Map orderInfo = {};

  //获取数据
  @override
  void initState() {
    super.initState();
    setState(() {
      orderInfo = widget.orderInfo;
    });
  }

//判断订单状态
  getOrderStatus(state) {
    switch (state) {
      case 1:
        return '待付款';
      case 2:
        return '待发货';
      case 3:
        return '待收货';
      case 4:
        return '待评价';
      case 5:
        return '已完成';
      default:
        return '未知';
    }
  }

  @override
  Widget build(BuildContext context) {
    //设置路由跳转
    return GestureDetector(
      onTap: () {
        //跳转方式1
        // Navigator.push(context, MaterialPageRoute(
        //   builder: (BuildContext context) {
        //     //传递参数
        //     return DetailShop(
        //       goodsId: '123',
        //     );

        //   },
        // ));
        //跳转方式2
        Navigator.pushNamed(context, '/detail',
            arguments: {'goodsId': '123', 'name': '张大喜'});
      },
      //内容
      child: Container(
        margin: const EdgeInsets.only(top: 20),
        width: MediaQuery.of(context).size.width,
        height: 220,
        color: const Color.fromARGB(255, 190, 232, 250),
        child: Column(
          children: [
            //顶部下单时间
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(orderInfo['createTime']),
                Text(
                  getOrderStatus(orderInfo['orderState']),
                  style: const TextStyle(
                      color: Color.fromARGB(255, 255, 228, 148)),
                )
              ],
            ),
            const SizedBox(
              height: 10,
            ),
            //商品详情部分
            Row(
              children: [
                //图片
                Image.network(
                  orderInfo['image'],
                  width: 100,
                ),
                const SizedBox(width: 10),
                //右侧商品文案
                Expanded(
                  //弹性布局
                  child: Column(
                    //文案列表
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(children: [
                        Expanded(
                          child: Text(
                            orderInfo['name'],
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        //数量
                        Padding(
                          padding: const EdgeInsets.only(left: 10, right: 10),
                          child: Text('x ${orderInfo['totalNum']}'),
                        )
                      ]),
                      const SizedBox(height: 10),
                      Text(
                        orderInfo['attrsText'],
                        style: const TextStyle(
                            color: Color.fromARGB(255, 152, 152, 151)),
                      ),
                      const SizedBox(height: 10),
                      Text('￥${orderInfo['curPrice']}'),
                    ],
                  ),
                )
              ],
            ),
            //底部合计与按钮
            Container(
              width: MediaQuery.of(context).size.width,
              padding: const EdgeInsets.only(left: 10, right: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  //底部合计与按钮
                  Text('合计:￥${orderInfo['totalNum'] * orderInfo['curPrice']}'),
                  const SizedBox(height: 10),
                  Container(
                    padding: const EdgeInsets.all(5),
                    decoration: BoxDecoration(
                        border: Border.all(color: Colors.black, width: 2)),
                    child: const Text('再次购买'),
                  ),
                  // ElevatedButton(onPressed: () {}, child: Text('再次购买'))
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
